package site.laodengtou.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import site.laodengtou.mapper.StudentMapper;
import site.laodengtou.pojo.Question;
import site.laodengtou.pojo.Student;
import site.laodengtou.pojo.Test;
import site.laodengtou.pojo.Works;
import site.laodengtou.service.StudentService;
import site.laodengtou.util.Msg;

@Service
public class StudentServiceImpl implements StudentService{

	@Autowired
	StudentMapper studentMapper;
	public Msg selStuCountByLogin(String ksh, String password) {
		Msg msg;
		// TODO Auto-generated method stub
		Student student = new Student();
		student.setKsh(ksh);
		student.setPassword(password);
		if(studentMapper.selCountByLogin(student)>=1) {
			
			msg = Msg.success();
			return msg;
		}else {
			msg = Msg.fail();
			msg.setMsg("用户名或密码错误");
			return msg;
		}
	}
	
	/**
	 * 查询考试
	 */
	public List<Works> selWorks() {
		// TODO Auto-generated method stub
		List<Works> list = studentMapper.selWorks();
		return list;	
	}
    
	/**
	 * 随机获取5道题
	 */
	public List<Question> getQuestions() {
		// TODO Auto-generated method stub
		return studentMapper.selQuestions();
	}
  
	/**
	 * 获取考试通过次数
	 */
	public int selCountPass(int id, String khs) {
		// TODO Auto-generated method stub
		
		return studentMapper.selCountBypass(id, khs);
		
	}
	/**
	 * 获取提交次数
	 */

	public int selCountSubmit(int id, String khs) {
		// TODO Auto-generated method stub
		return studentMapper.selCountSubmit(id, khs);
	}
    /**
     * 获取提交的最高分数
     */
	public int selMaxGradeSubmit(int id, String khs) {
		// TODO Auto-generated method stub
		return studentMapper.selMaxGradeSubmit(id, khs);
	}

	public int selCountSubmitQuestion(int id, String khs) {
		// TODO Auto-generated method stub
		return studentMapper.selCountSubmitQuestion(id,khs);
	}

	public int selCountPassQuestion(int id, String khs) {
		// TODO Auto-generated method stub
		return studentMapper.selCountPassQuestion(id, khs);
	}

	public int selMaxGradeQuestion(int id, String khs) {
		// TODO Auto-generated method stub
		String maxgrade =  studentMapper.selMaxGradeQuestion(id, khs);
		if(maxgrade==null||maxgrade.equals("")) {
		   return 0;
		}else {
			return Integer.parseInt(maxgrade);
		}
	}

	public Question selQuestionByid(int id) {
		// TODO Auto-generated method stub
		return studentMapper.selQuestionByid(id);
	}

	public int insSubmitcode(int workid, int questionid, String ksh, String code) {
		// TODO Auto-generated method stub
		return studentMapper.insSubmitCode(workid, questionid, ksh, code);
	}

	public int insTest(Test test) {
		// TODO Auto-generated method stub
		System.out.println(test.getId());
		System.out.println(test.getQuestionid1());
		System.out.println(test.getQuestionid2());
		System.out.println(test.getQuestionid3());
		System.out.println(test.getQuestionid4());
		System.out.println(test.getQuestionid5());
		System.out.println(test.getWorkid());
	    studentMapper.insTest(test);
	    return test.getId();//返回id主键值
	}

	public Test selTestById(int id) {
		// TODO Auto-generated method stub
		return studentMapper.selTestByid(id);
	}
	
    
	

}
